package com.cy.pj.goods.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;
import org.springframework.web.bind.annotation.*;

@Controller //@Service,@Component
@RequestMapping("/goods/")
public class GoodsController {
    //has a+di
    @Autowired
    private GoodsService goodsService;
    @GetMapping("doGoodsUI")
    public String doGoodsUI(Model model) {
        //调用业务层方法获取商品信息
        List<Goods> list= goodsService.findGoods();
        //将数据存储到请求作用域
        model.addAttribute("list", list);
        return "goods";//viewname
    }

    @RequestMapping("doDeleteById/{id}")
    public String doDeleteById(@PathVariable Integer id){
        goodsService.doDeleteById(id);
        return "redirect:/goods/doGoodsUI";
    }

    @GetMapping("doGoodsAddUI")
    public String doGoodsAddUI(){
        return "goodsAdd";
    }

    @PostMapping("doSaveGoods")
    public String doSaveGoods(Goods entity){
        goodsService.insertGoods(entity);
        return "redirect:/goods/doGoodsUI";
    }

    @GetMapping("doFindById/{id}")
    public String doFindById(@PathVariable Integer id,Model model){
        Goods goods = goodsService.findById(id);
        model.addAttribute("g",goods);
        return "goodsUpdate";
    }

    @RequestMapping("doUpdateGoods")
    public String doUpdateGoods(Goods entity){
        goodsService.updateGoods(entity);
        return "redirect:/goods/doGoodsUI";
    }
}